﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using NHibernate.Criterion;
using ZLCMS.Data.Common;
using NHibernate;
using ZLCMS.Data.Model;
using System.Collections.Generic;
using ZLCMS.Business;
using ZLCMS.Business.Commands.SessionCommands;
using DotNet.Common.Command;
using ZLCMS.Web.Wap.Code;

namespace ZLCMS.Web.MemberUser
{
    public partial class ChgPasswd : WebMemberPageBase
    {
        protected MemberInfo _memberDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            _memberDb = Master.LoadMember();

            MemberCategory1.MenuIndex = "xgmm";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SaveUser();
        }

        protected void SaveUser()
        {
            ClientScriptManager csm = this.Page.ClientScript;

            if (tb_old.Text == tb_new1.Text)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('新密码不能和旧密码相同。');", true);
                return;
            }

            if (tb_new1.Text != tb_new2.Text)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('两次输入密码不匹配。');", true);
                return;
            }

            String oldMd5 = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_old.Text.Trim(), "MD5").ToLower();

            if (oldMd5 != _memberDb.Password)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('原密码不正确。');", true);
                return;
            }

            ChangePasswordCommandParameter cmdParam = new ChangePasswordCommandParameter();

            cmdParam.UserCode = _memberDb.AccountId;
            cmdParam.OldPassword = oldMd5;
            cmdParam.NewPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_new1.Text.Trim(), "MD5").ToLower();

            CommandResult result = new ChangePasswordCommand().Execute(cmdParam) as CommandResult;

            if (result.Error)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('" + result.ErrorMsg + "');", true);
                return;
            }

            csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功，请牢记您的新密码。');window.location.href='/MemberUser/Default.aspx'", true);
        }
    }
}
